package com.boot.es.config;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author zhangsw
 * @Package com.boot.config
 * @date 2020/12/1 19:04
 */

@Configuration
@Slf4j
public class ElasticsearchConfig {

    @Bean
    public TransportClient client() {
        TransportClient client=null;
        try {
            //注意这儿是tcp链接 是9300
            TransportAddress transportAddress = new TransportAddress(InetAddress.getByName("10.27.14.9"), 9300);
            Settings settings = Settings.builder()
                    .put("client.transport.sniff", true)//增加嗅探机制，找到ES集群
                    .put("thread_pool.search.size", 2)//增加线程池个数，暂时设为5
                    .put("cluster.name", "elasticsearch").build();
            client = new PreBuiltTransportClient(settings);
            client.addTransportAddress(transportAddress);
        }catch (Exception e){
            e.printStackTrace();
            log.info("es client 初始化失败");
        }
        return client;
    }

}
